package com.reconcile.phone.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reconcile.phone.business.domain.RpPhoneCode;
import com.reconcile.phone.business.domain.RpPhoneOrder;
import com.reconcile.phone.business.domain.bo.RpPhoneOrderBo;
import com.reconcile.phone.business.domain.vo.RpPhoneCodeVo;
import com.reconcile.phone.business.domain.vo.RpPhoneOrderVo;
import com.reconcile.phone.business.mapper.RpPhoneCodeMapper;
import com.reconcile.phone.business.mapper.RpPhoneOrderMapper;
import com.reconcile.phone.business.service.RpPhoneOrderService;
import com.reconcile.phone.common.core.domain.PageQuery;
import com.reconcile.phone.common.helper.LoginHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Author: liuliyou
 * Date: 2025/8/7
 */
@Service
@RequiredArgsConstructor
public class RpPhoneOrderServiceImpl implements RpPhoneOrderService {

    private final RpPhoneOrderMapper rpPhoneOrderMapper;
    private final RpPhoneCodeMapper rpPhoneCodeMapper;

    /**
     * 新增订单
     *
     * @param bo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RpPhoneOrder insertPhoneOrder(RpPhoneOrderBo bo) {
        //TODO 查询目标手机号没有绑定的虚拟号
        LambdaQueryWrapper<RpPhoneOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RpPhoneOrder::getTargetPhone, bo.getTargetPhone());
        //根据拨打手机号 查询使用过的虚拟号
        List<String> virtualPhoneIds = new ArrayList<>();
        List<RpPhoneOrderVo> phoneOrderVoList = rpPhoneOrderMapper.selectVoList(queryWrapper);
        if (CollectionUtil.isNotEmpty(phoneOrderVoList)) {
            virtualPhoneIds = phoneOrderVoList.stream().map(RpPhoneOrderVo::getVirtualPhone).collect(Collectors.toList());
        }
        //查询可用虚拟号
        LambdaQueryWrapper<RpPhoneCode> queryWrapperOne = new LambdaQueryWrapper<>();
        queryWrapperOne.notIn(ObjectUtil.isNotEmpty(virtualPhoneIds), RpPhoneCode::getPhoneNum, virtualPhoneIds);
        queryWrapperOne.last("limit 1");
        RpPhoneCodeVo rpPhoneCodeVo = rpPhoneCodeMapper.selectVoOne(queryWrapperOne);
        if (ObjectUtil.isNull(rpPhoneCodeVo)) {
            throw new RuntimeException("下单失败，中间号匹配失败！");
        }
        queryWrapper.eq(RpPhoneOrder::getTargetPhone, bo.getTargetPhone());
        RpPhoneOrder rpPhoneOrder = BeanUtil.toBean(bo, RpPhoneOrder.class);
        int result = rpPhoneOrderMapper.insert(rpPhoneOrder);
        if (result > 0) {
            rpPhoneOrder.setVirtualPhone(rpPhoneCodeVo.getPhoneNum());
            return rpPhoneOrder;
        } else {
            throw new RuntimeException("下单失败！");
        }
    }

    /**
     * 订单分页查询
     *
     * @param bo
     * @return
     */
    @Override
    public List<RpPhoneOrderVo> selectPhoneOrderList(RpPhoneOrderBo bo) {

        LambdaQueryWrapper<RpPhoneOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RpPhoneOrder::getCreateBy, LoginHelper.getUserId());
        queryWrapper.orderByDesc(RpPhoneOrder::getCreateTime);
        queryWrapper.last("limit 20");
        List<RpPhoneOrderVo> list = rpPhoneOrderMapper.selectVoList(queryWrapper);
        return list;
    }

    /**
     * 后台分页查询
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public Page<RpPhoneOrderVo> selectSysOrderList(RpPhoneOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RpPhoneOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(RpPhoneOrder::getCreateTime);
        Page<RpPhoneOrderVo> pageList = rpPhoneOrderMapper.selectVoPage(pageQuery.build(), queryWrapper);
        return pageList;
    }

    /**
     * 订单详细信息
     *
     * @param phoneOrderId
     * @return
     */
    @Override
    public RpPhoneOrderVo queryPhoneOrderById(Long phoneOrderId) {
        return rpPhoneOrderMapper.selectVoById(phoneOrderId);
    }
}
